import java.io.IOException;
import java.text.*;
import java.util.*;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.OutputDocument;
import net.htmlparser.jericho.Source;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.util.Calendar;

public class Article {
	//int m_aId;
	private String m_publishTime;
	private String m_articleTitle;
	private String m_articleContents;
	private String m_articleLink;
	private String m_articleThumb;
	private int  m_articleId;
	private int m_categoryId;
	

	public Article(String _pubDate, String _title, String _link, String _thumb, int _category)
	{
		this.m_publishTime = convertDate(_pubDate);
		this.m_articleTitle = _title;
		this.m_articleLink = _link;
		this.m_articleThumb = _thumb;
		this.m_articleId = Integer.parseInt(_link.split("/")[6]) ;
		this.m_categoryId = _category;
		m_articleContents = this.getContents();
		
		//System.out.println(this);
		//System.out.println(m_articleId);
		
	}
	
	
	public String getContents()
	{
		HttpClient parseHttpClient = new DefaultHttpClient();
		HttpGet parseHttpGet = new HttpGet(this.m_articleLink);
		OutputDocument outputRssDoc = null;
		HttpEntity parseResEntity;
		
		try {
			parseResEntity = parseHttpClient.execute(parseHttpGet).getEntity();
			String rssDocContents = EntityUtils.toString(parseResEntity);
			
			//���ڵ� ��ȯ
			//rssDocContents = new String(rssDocContents.getBytes("ISO-8859-1"), "UTF-8");
			
			Element removeTarget = null;
			
			Source parserRssDoc = new Source(rssDocContents);
			Element article_txt = parserRssDoc.getFirstElementByClass("article_txt");
		   	if(article_txt == null) return null;
			
		   	outputRssDoc = new OutputDocument(article_txt);
			
			if((removeTarget = article_txt.getFirstElement("iframe")) != null)
				outputRssDoc.remove(removeTarget);

			/*//��������...
			if((removeTarget = article_txt.getFirstElement("txt_ad")) != null)
				outputRssDoc.remove(removeTarget);
			if((removeTarget = article_txt.getFirstElementByClass("sub_cont_AD08")) != null)
				outputRssDoc.remove(removeTarget);
			if((removeTarget = article_txt.getFirstElementByClass("sub_cont_AD09")) != null)
				outputRssDoc.remove(removeTarget);
			*/
			/*if((removeTarget = article_txt.getFirstElementByClass("outsideBox")) != null)
				outputRssDoc.remove(removeTarget);*/
			outputRssDoc.remove(article_txt.getAllElements("iframe"));
			
			outputRssDoc.remove(article_txt.getFirstElementByClass("title_foot"));
			outputRssDoc.remove(article_txt.getFirstElementByClass("article_relation"));
			outputRssDoc.remove(article_txt.getFirstElementByClass("t_sns"));
			outputRssDoc.remove(article_txt.getFirstElementByClass("t_snsBtn"));
			outputRssDoc.remove(article_txt.getFirstElementByClass("t_option"));

				
			//System.out.println(this.m_articleContents);
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return  outputRssDoc.toString();		
	}
	
	//date format converter 
	private String convertDate(String time)
	{
		String[] splitedTime = time.split(" ");
		String inputTimeStamp = "";
		String outputTimeStamp = null;
		SimpleDateFormat inputFormat = new SimpleDateFormat("dd MMM yyyy kk:mm:ss", Locale.ENGLISH);
		SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss",Locale.KOREAN);
			
		for(int i=1; i<splitedTime.length-1; i++)
		{
			inputTimeStamp += splitedTime[i]+" ";
		}
			
		try
		{
			outputTimeStamp = outputFormat.format(inputFormat.parse(inputTimeStamp));
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		
		return outputTimeStamp;		
	}

	
	//toString (testing)
	public String toString()
	{
		return m_publishTime+"\n";
	}
	
	
	//getter
	public String getPublishTime()
	{
		return m_publishTime;
	}
	public String getArticleTitle()
	{
		return m_articleTitle;
	}

	public String getArticleContents()
	{
		return m_articleContents;
	}

	public String getAarticleLink()
	{
		return m_articleLink;
	}

	public String getArticleThumb()
	{
		return m_articleThumb;
	}

	public int getAarticleId()
	{
		return m_articleId;
	}
	public int getCategoryId()
	{
		return m_categoryId;
	}
}
